package org.study.hadoop.md.ch4;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.study.hadoop.md.util.MRDPUtils;

import java.io.IOException;
import java.util.Map;

public class Binning extends Configured implements Tool {

    public static class BinningMapper extends Mapper<Object, Text, Text, NullWritable> {
        private MultipleOutputs<Text, NullWritable> mos = null;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            mos = new MultipleOutputs<Text, NullWritable>(context);
        }

        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            Map<String, String> parsed = MRDPUtils.transformXmlToMap(value.toString());
            String rawtag = parsed.get("Tags");
            if (rawtag == null) {
                return;
            }
            if (rawtag.equalsIgnoreCase("hadoop")) {
                mos.write("bins", value, NullWritable.get(), "hadoop-tag");
            }
            if (rawtag.equalsIgnoreCase("hbase")) {
                mos.write("bins", value, NullWritable.get(), "hbase-tag");
            }
            if (rawtag.equalsIgnoreCase("kafka")) {
                mos.write("bins", value, NullWritable.get(), "kafka-tag");
            }
            if (rawtag.equalsIgnoreCase("c")) {
                mos.write("bins", value, NullWritable.get(), "c-tag");
            }
            if (rawtag.equalsIgnoreCase("sql")) {
                mos.write("bins", value, NullWritable.get(), "sql-tag");
            }
            if (rawtag.equalsIgnoreCase("python")) {
                mos.write("bins", value, NullWritable.get(), "python-tag");
            }
            if (rawtag.equalsIgnoreCase("java")) {
                mos.write("bins", value, NullWritable.get(), "java-tag");
            }
            if (rawtag.equalsIgnoreCase("flume")) {
                mos.write("bins", value, NullWritable.get(), "flume-tag");
            }
            if (rawtag.equalsIgnoreCase("hive")) {
                mos.write("bins", value, NullWritable.get(), "hive-tag");
            }
            if (rawtag.equalsIgnoreCase("spark")) {
                mos.write("bins", value, NullWritable.get(), "spark-tag");
            }
            String post = parsed.get("Body");
            if (post == null) {
                return;
            }
            if (post.toLowerCase().contains("maven")) {
                mos.write("bins", value, NullWritable.get(), "maven-post");
            }
        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            mos.close();
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Path outPath = new Path("out");
        Path inPath = new Path("post.txt");
        FileSystem fs = outPath.getFileSystem(getConf());
        if (fs.exists(outPath)) {
            fs.delete(outPath, true);
        }
        Job job = Job.getInstance(getConf(), "Binning");
        job.setJarByClass(Binning.class);
        job.setMapperClass(BinningMapper.class);
        job.setNumReduceTasks(0);
        FileInputFormat.addInputPath(job, inPath);
        FileOutputFormat.setOutputPath(job, outPath);
        MultipleOutputs.addNamedOutput(job, "bins", TextOutputFormat.class, Text.class, NullWritable.class);
        MultipleOutputs.setCountersEnabled(job, true);
        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new Binning(), args);
        System.exit(res);
    }
}
